package pers.qianyu.module.core.comm;


import io.swagger.annotations.*;
import pers.qianyu.exception.*;

import java.time.*;
import java.time.format.*;

/**
 * Lamb 项目统一接口返回值
 *
 * @param <T> 返回数据类型
 * @author mizzle_rain
 */
@ApiModel("通用接口返回对象")
public class HttpResult<T> {
    @ApiModelProperty(required = true, notes = "结果码", example = "0")
    private int code;
    @ApiModelProperty(required = true, notes = "返回信息", example = "操作成功")
    private String message;
    @ApiModelProperty(required = true, notes = "返回数据", example = "{\"id\":2001}")
    private T data;
    @ApiModelProperty(required = true, notes = "时间戳", example = "2020-10-08 09:07:34")
    private String timestamp;

    {
        timestamp = LocalDateTime.now()
                .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    public static HttpResult<?> error(Integer code, String message) {
        HttpResult<?> httpResult = new HttpResult<>();
        httpResult.setCode(code);
        httpResult.setMessage(message);
        return httpResult;
    }

    public static HttpResult<?> error(String message) {
        return error(5000, message);
    }

    public static HttpResult<?> error(ILambErrorCodeEnum errorCode) {
        return error(errorCode.getCode(), errorCode.getMessage());
    }

    public static HttpResult<?> error(LambBaseException e) {
        return error(e.getCode(), e.getMessage());
    }

    public static HttpResult<?> ok() {
        HttpResult<?> httpResult = new HttpResult<>();
        httpResult.setMessage("请求成功");
        return httpResult;
    }

    public static <T> HttpResult<T> okWithData(T t) {
        HttpResult<T> httpResult = new HttpResult<T>();
        httpResult.setMessage("请求成功");
        httpResult.setData(t);
        return httpResult;
    }

    public static HttpResult<?> okWithMessage(String message) {
        HttpResult<?> httpResult = new HttpResult<>();
        httpResult.setMessage(message);
        return httpResult;
    }


    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public HttpResult<T> setMessage(String message) {
        this.message = message;
        return this;
    }

    public T getData() {
        return data;
    }

    public HttpResult<T> setData(T data) {
        this.data = data;
        return this;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }
}